package net.jwtools.tmt.dao.hibernate;

import java.util.List;

import net.jwtools.tmt.domain.Person;
import net.jwtools.tmt.domain.Territory;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class TerritoryHibernateDAO extends AbstractSpringDao {

	public Territory findById(Long id) {
		return (Territory) super.find(Territory.class, id);
	}

	public void saveOrUpdate(Territory fieldTerritory) {
		super.saveOrUpdate(fieldTerritory);
	}

	public void delete(Territory Person) {
		super.delete(Person);
	}

	@SuppressWarnings("unchecked")
	public List<Territory> findAll() {
		return (List<Territory>) super.findAll(Territory.class);
	}
	
	public Territory findByNumber(Integer number) {
		DetachedCriteria crit = DetachedCriteria.forClass(Territory.class);
		crit.add(Restrictions.eq("number",number));
		return (Territory)super.findByCriteria(crit).get(0);
	}
	
	public Territory findFullById(Long id) {
		DetachedCriteria crit = DetachedCriteria.forClass(Territory.class);
		crit.add(Restrictions.eq("id",id))
		.setFetchMode("map", FetchMode.JOIN)
		.setFetchMode("surroundingTerritories", FetchMode.JOIN);
		return (Territory)super.findByCriteria(crit).get(0);
	}
}
